package com.jiyun.jiyunmall.product.controller;

import com.jiyun.jiyunmall.common.annotation.LogOperation;
import com.jiyun.jiyunmall.common.constant.Constant;
import com.jiyun.jiyunmall.common.page.PageData;
import com.jiyun.jiyunmall.common.utils.ExcelUtils;
import com.jiyun.jiyunmall.common.utils.Result;
import com.jiyun.jiyunmall.common.validator.AssertUtils;
import com.jiyun.jiyunmall.common.validator.ValidatorUtils;
import com.jiyun.jiyunmall.common.validator.group.AddGroup;
import com.jiyun.jiyunmall.common.validator.group.DefaultGroup;
import com.jiyun.jiyunmall.common.validator.group.UpdateGroup;
import com.jiyun.jiyunmall.product.dto.BrandDTO;
import com.jiyun.jiyunmall.product.dto.CategoryBrandRelationDTO;
import com.jiyun.jiyunmall.product.dto.CategoryDTO;
import com.jiyun.jiyunmall.product.excel.CategoryBrandRelationExcel;
import com.jiyun.jiyunmall.product.service.BrandService;
import com.jiyun.jiyunmall.product.service.CategoryBrandRelationService;
import com.jiyun.jiyunmall.product.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author boge 71164376@qq.com
 * @since 1.0.0 2022-08-04
 */
@RestController
@RequestMapping("product/categorybrandrelation")
@Api(tags="")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BrandService brandService;
/*
根据brandName模糊查询，返回这个格式的list：[{value:"1",label:"a"}
            List<Map<String,String>>
 */
    @GetMapping("/likeBrandName")
    @ApiOperation("分页")
    public Result<List<Map<String,String>>> likeBrandName(String brandName){
        List<Map<String,String>> resultList =   brandService.likeBrandName(brandName);
        return new Result<List<Map<String,String>>>().ok(resultList);
    }


    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<PageData<CategoryBrandRelationDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<CategoryBrandRelationDTO> page = categoryBrandRelationService.page(params);

        return new Result<PageData<CategoryBrandRelationDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<CategoryBrandRelationDTO> get(@PathVariable("id") Long id){
        CategoryBrandRelationDTO data = categoryBrandRelationService.get(id);
        // 根据当前分类关系中的categoryId查询它的一级分类id和二级分类id
        // 然后，三者以[1,2,3] 数组的形式，放到data中给前端。
        CategoryDTO level3 = categoryService.get(data.getCategoryId());
        CategoryDTO level2 = categoryService.get(level3.getParentCid());
        Long level1Id = level2.getParentCid();
        Long level2Id = level2.getCatId();
        Long level3Id = level3.getCatId();
        Long[] longs = {level1Id,level2Id,level3Id};
        //封装三级分类id路径
        data.setCatIdPath(longs);
        return new Result<CategoryBrandRelationDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result save(@RequestBody CategoryBrandRelationDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        // 根据brandId、catId查询brand Name、catName
        BrandDTO brandDTO = brandService.get(dto.getBrandId());
        CategoryDTO categoryDTO = categoryService.get(dto.getCategoryId());

        dto.setBrandName(brandDTO.getName());
        dto.setCategoryName(categoryDTO.getName());
        categoryBrandRelationService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    public Result update(@RequestBody CategoryBrandRelationDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        BrandDTO brandDTO = brandService.get(dto.getBrandId());
        CategoryDTO categoryDTO = categoryService.get(dto.getCategoryId());
        dto.setBrandName(brandDTO.getName());
        dto.setCategoryName(categoryDTO.getName());
        categoryBrandRelationService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        categoryBrandRelationService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<CategoryBrandRelationDTO> list = categoryBrandRelationService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, CategoryBrandRelationExcel.class);
    }

}